package test5.lupeng.jf.com.teamdemo.activitys;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;

import com.google.gson.Gson;

import java.io.IOException;
import java.util.ArrayList;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import test5.lupeng.jf.com.teamdemo.R;
import test5.lupeng.jf.com.teamdemo.adapter.AttentionAdapter;
import test5.lupeng.jf.com.teamdemo.bean.response.ResponseGuan;
import test5.lupeng.jf.com.teamdemo.untils.HTTPUtils;

//关注列表
public class AttentionActivity extends AppCompatActivity {

    private static final String TAG = "AttentionActivity" ;
    private String otherId;
    private RecyclerView attenVp;
    ArrayList<ResponseGuan.ResultsBean> results;
    AttentionAdapter adapter;
    private int guanzhuNumber = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activitys_attention);

        //拿到其他用户的Id
        otherId = getIntent().getStringExtra("otherId");
        attenVp = (RecyclerView) findViewById(R.id.AttenVp);

        results = new ArrayList<>();

        //请求关注列表
        RequestAtten();

        adapter = new AttentionAdapter(results);
        attenVp.setLayoutManager(new LinearLayoutManager(this));
        attenVp.setAdapter(adapter);

    }

    //请求关注列表的方法
    public void RequestAtten(){

        String url = "https://api.leancloud.cn/1.1/users/"+otherId+"/followees";
        Request request = HTTPUtils.requestGet(url);
        OkHttpClient client = new OkHttpClient();
        client.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {

            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String result = response.body().string();
                Log.e(TAG, "onResponse: " + result);
                results.clear();
                ResponseGuan bean = new Gson().fromJson(result, ResponseGuan.class);
                results.addAll(bean.getResults());
                guanzhuNumber = results.size();

                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        adapter.notifyDataSetChanged();
                    }
                });
            }
        });
    }



    //返回按钮
    public void return2(View view){
        Intent intent = new Intent();
        intent.putExtra("guanzhuNumber", guanzhuNumber);
        //System.out.println(guanzhuNumber);
        setResult(RESULT_OK,intent);
        finish();
    }

}
